﻿#include "GameManager.h"

CGameManager::CGameManager()
{
	//Cấp phát bộ nhớ cho các đổi tượng 
	_dxInput=new CDxInput();
	_gr=new CGraphics();
	_sound=new CSound();
	CGameState::state=GameState::Menu;
}
CGameManager::~CGameManager()
{
}
bool CGameManager::Init(HINSTANCE hInst,HWND hwnd)
{
	if(!_gr->Init_Direct3D(hwnd,WINDOW_WIDTH,WINDOW_HEIGHT))
		return false;
	//initialize direct input
	if(!_dxInput->Init_DirectInput(hInst,hwnd))
	{
		MessageBox(hwnd, "Error initializing the direct input", "Error", MB_OK);
		return false;
	}
	//initialize keyboard
	if(!_dxInput->Init_Keyboard(hwnd))
	{
		MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
		return false;
	}
	//initialize Mouse
	if(!_dxInput->Init_Mouse(hwnd))
	{
		MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
		return false;
	}
	//initialize Sound
	if(!_sound->Init(hwnd))
	{
		MessageBox(hwnd, "Error initializing the sound", "Error", MB_OK);
		return false;
	}
	//Truyền các đối tượng cho con tro state hien tai
	CGameState::_gr=_gr;
	CGameState::_sound=_sound;
	CGameState::_dxInput=_dxInput;
	//initialize game state
	LPD3DXSPRITE _spriteHandler;
	D3DXCreateSprite(_gr->_d3ddev,&_spriteHandler);
	CSprites::_spriteHandler=_spriteHandler;

	_gameMenu=CGameMenu::SetInstance();//Singleton
	_gameMenu->Game_Init();
	_gamePlay=CGamePlay::SetInstance(hwnd);
	_gamePlay->Game_Init();
	return true;
}
void CGameManager::Run(HWND hwnd,DWORD tickPerFrame)
{
	//Chuyen doi qua lai giua cac satate
	switch(CGameState::state)
	{
	case GameState::Menu:
		{
			_gamePointer=_gameMenu;
			break;
		}
	case GameState::LoadGame:
		{
			_gamePlay->Game_Load(true);
		}
	case GameState::Play:
		{
			CGameState::state=GameState::Play;
			_gamePointer=_gamePlay;
			break;
		}
	default:
		{
			_gamePointer=_gameMenu;
			break;
		}
	}
	_gamePointer->Game_Run(hwnd,tickPerFrame);
}	
void CGameManager::End()
{
	//Giai pong cac doi tuong
	delete _gr;
	delete _dxInput;
	_gameMenu->Game_End();
	_gamePlay->Game_End();
}